package com.useeinfo.oa.open.admin.repair;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.CommonUtils;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.DictInfoBiz;
import com.useeinfo.oa.modules.base.biz.EnterpriseBiz;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.repair.biz.TroubleCatBiz;
import com.useeinfo.oa.modules.repair.entity.TroubleCat;
import com.useeinfo.oa.modules.security.biz.OrganizationBiz;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

/**
 * @Auther: uajuan.wang
 * @Date: 2018/12/6 13:03
 * @Description: 故障分类action
 */
@Controller
@RequestMapping("/web/repair/troubleCat")
public class TroubleCatManagerAction {
    @Autowired
    private TroubleCatBiz troubleCatBiz;
    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @Autowired
    private OrganizationBiz organizationBiz;
    @Autowired
    private DictInfoBiz dictInfoBiz;

    @RequestMapping("/list")
    public ModelAndView list() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/admin/troubleCat/troubleCatList");

        return modelAndView;
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                               @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                               @RequestParam(value = "searchCode", required = false) String searchCode,
                               @RequestParam(value = "searchSerial", required = false) String searchSerial,
                               @RequestParam(value = "searchName", required = false) String searchName,
                               @RequestParam(value = "orderBy", required = false) String orderBy,HttpServletRequest request){
        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("searchCode", searchCode);
        queryParam.getSqlMap().put("searchSerial", searchSerial);
        queryParam.getSqlMap().put("searchName", searchName);
        if (StringUtils.isNotBlank(orderBy)) {
            queryParam.setSqlOrder("order by " + orderBy);
        }
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId()+"");
        }
        return troubleCatBiz.findJSONList(queryParam);
    }

    @RequestMapping("/editPage")
    public ModelAndView editPage(String id) {

        ModelAndView modelAndView = new ModelAndView();
        TroubleCat troubleCat = troubleCatBiz.findModel(StringConverters.null2Long(id));
        modelAndView.addObject("troubleCat", troubleCat);
        modelAndView.setViewName("pages/admin/troubleCat/troubleCatEditPart");

        return modelAndView;
    }

    /**
     * 新增故障分类
     * @param troubleCat
     * @param request
     * @param repairClassifys
     * @param facilityCats
     * @param organizations
     * @return
     */
    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public ResultDto addOrUpdate(TroubleCat troubleCat,HttpServletRequest request,
                                 @RequestParam(value = "select_repairClassify[]", required = false) List<String>repairClassifys ,
                                 @RequestParam(value = "select_facilityCat[]", required = false) List<String>facilityCats ,
                                 @RequestParam(value = "select_organization[]", required = false) List<String> organizations) {
        return troubleCatBiz.saveUpdate(troubleCat,request,organizations,facilityCats,repairClassifys);
    }

    /**
     * 删除故障分类
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResultDto delete(String id) {
        troubleCatBiz.deleteLogic(troubleCatBiz.findModel(StringConverters.null2Long(id)));
        return ResultUtil.success();
    }
}
